// packageF/others/wisdom/wisdomBind/wisdomBind.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    option: "",
    //手机号码
    mobile: "",
    // 手机验证码
    smsMobile: "",
    // 图形验证码
    imgcode: "",
    // 是否开启图形验证码
    imgcodeShow: false,
    btnText: "获取验证码",
    timer: null,
    btnTag: false,
    // 姓名
    userName: "",
    code: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.option) {
      this.setData({
        option: options.option
      })
      wx.setNavigationBarTitle({
        title: options.option == 'phone' ? '绑定手机' : '实名认证',
      })
    }
    if (options.to) {
      this.setData({
        to: options.to
      })
    }
  },
  confrimBtn() {
    let url = "";
    let json = {};
    if (this.data.option == "phone") {
      url = app.getNetAddresss("plugin.eplus-pay.frontend.mobile.smsConfirm");
      json = {
        mobile: this.data.mobile,
        code: this.data.smsMobile
      };
    } else {
      url = app.getNetAddresss("plugin.eplus-pay.frontend.name.nameAuth");
      json = {
        name: this.data.userName,
        identity_no: this.data.code
      };
    }
    app._getNetWork({
      url: url,
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          wx.showModal({
            showCancel: false,
            content: res.msg,
            success: () => {
              if (this.data.to == 'pay') {
                wx.navigateBack({
                  delta: 1,
                })
              } else {
                wx.redirectTo({
                  url: '/packageF/others/wisdom/wisdomBindTask/wisdomBindTask',
                })
              }
            }
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  // 验证发送短信验证码
  checkCodeMethod() {
    this.firstNext();
  },
  firstNext() {
    let url = app.getNetAddresss("plugin.eplus-pay.frontend.mobile.smsCode");
    app._getNetWork({
      url: url,
      data:{mobile: this.data.mobile},
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.sendCodeAnimation();
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  // 倒计时
  sendCodeAnimation() {
    const TIME_COUNT = 60;
    if (!this.data.timer) {
      this.data.count = TIME_COUNT;
      wx.showToast({
        title: '验证码已发送',
        icon: 'success',
        duration: 1000
      })
      this.data.timer = setInterval(() => {
        if (this.data.count > 0 && this.data.count <= TIME_COUNT) {
          this.data.count--;
          this.setData({
            btnTag: true,
            btnText: this.data.count + "s"
          })
        } else {
          this.setData({
            btnTag: false,
            btnText: '获取验证码'
          })
          clearInterval(this.data.timer);
          this.data.timer = null;
        }
      }, 1000);
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})